<?php
	class ResumeModel extends Model{
		
		public function getmyresume(){
			$uid = $_SESSION['member']['uid'];
			$myresume = D('member_info')->where('uid = '.$uid)->find();
			if(!empty($myresume)){
				$myjobexper = D('exper')->where(array('uid'=>$uid,'is_del'=>0))->findAll();
				$myresume['worklist'] = $myjobexper;
				$resume = D('resume')->where('uid = '.$uid)->find();
				$myresume['resume'] = $resume;
				$myresume['pic'] = get_img_url($myresume['face']);
			}
			return $myresume;
		}
		
		public function add_member($data){
			$uid = $_SESSION['member']['uid'];
			$data['uid'] = $uid;
			$data['status'] =1;
			$myresume = D('member_info')->where('uid = '.$uid)->find();
			if(!empty($myresume)){

				$result = D('member_info')->where('info_id = '.$myresume['info_id'])->save($data);
			}else{
				$result = D('member_info')->add($data);
			}
			return $result;
		}
		public function add_resume($data){
			$uid = $_SESSION['member']['uid'];
			$data['uid'] = $uid;
			$myresume = D('resume')->where('uid = '.$uid)->find();
			if(!empty($myresume)){
				$result = D('resume')->where('resume_id = '.$myresume['resume_id'])->save($data);
			}else{
				$result = D('resume')->add($data);
			}
			return $result;
		}
		
		public function addfile($file){
			if(D('jobs_pic')->add($file)){
				return true;
			}else{
				return false;
			}
		}

		public function delimg($imgid){
			$data['is_del'] = 1;
			if(D('jobs_pic')->where('fid = '.$imgid)->save($data)){
				if(D('file')->where('id = '.$imgid)->save($data)){
					return true;
				}else{
					return false;
				}
			}else{
				return false;
			}
		}
		
		public function getarea($type = 'pro',$aid = '0'){
			if($type == 'pro'){
				$return = D('area')->where('pid = 0')->findAll();
			}
			if($type == 'city'){
				$return = D('area')->where('pid = '.$aid)->findAll();
			}
			if($type == 'area'){
				$return = D('area')->where('pid = '.$aid)->findAll();
			}
			return $return;
		}

		public function getposition($hid){
			$return = D('position')->where('hid = '.$hid)->findAll();
			
			return $return;
		}
		
		public function addresume($post){
			//添加到jobs_resume
			$jobs_resume = array(
								'uid'=>$_SESSION['wxuser']['uid'],
								'ctime'=>time(),
								'zhuangtai'=>$post['zhuangtai'],
								'jingyan'=>$post['jingyan'],
								'yhyf'=>implode(',',$post['hangye']),
								'edubeigntime'=>$post['edubegintime'],
								'eduendtime'=>$post['eduendtime'],
								'eduxueli'=>$post['eduxueli'],
								'eduschool'=>$post['eduschool'],
								'myinfo'=>$post['myinfo'],
								'status'=>0,
								'is_del'=>0,
								'isOpen'=>1
							);
			$jobs_resume_status = D('resume')->add($jobs_resume);
			//添加到user_info
			$user_info = array(
								'uid'=>$_SESSION['wxuser']['uid'],
								'ctime'=>time(),
								'truename'=>$post['truename'],
								'xingbie'=>$post['xingbie'],
								'shengri'=>$post['shengri'],
								'shengao'=>$post['shengao'],
								'tizhong'=>$post['tizhong'],
								'hunyin'=>$post['hunyin'],
								'hpro'=>$post['hpro'],
								'hcity'=>$post['hcity'],
								'pro'=>$post['pro'],
								'city'=>$post['city'],
								'area'=>$post['area'],
								'xueli'=>$post['xueli'],
								'phone'=>$post['phone'],
								'status'=>0,
								'is_del'=>0,
								'recommend'=>0
							);
			$user_info_status = D('member_info')->add($user_info);
			//添加到jobs_exper
			$jobs_exper = array(
								'uid'=>$_SESSION['wxuser']['uid'],
								'ctime'=>time(),
								'jobbegin'=>$post['jobbegin'],
								'jobend'=>$post['jobend'],
								'jobzhiwei'=>$post['jobzhiwei'],
								'jobname'=>$post['jobname'],
								'is_del'=>0
							);
			$jobs_exper_status = D('exper')->add($jobs_exper);
			if($jobs_resume_status&&$user_info_status&&$jobs_exper_status){
				unset($_SESSION['resumestep']);
				return true;
			}else{
				return false;
			}
		}
								
		public function saveresume($step,$post){
			if($step==1){
				$jobs_resume = array(
									'ctime'=>time(),
									'zhuangtai'=>$post['zhuangtai'],
									'jingyan'=>$post['jingyan']
								);
				$jobs_resume_status = D('resume')->where('uid = '.$_SESSION['wxuser']['uid'])->save($jobs_resume);
				//添加到user_info
				$user_info = array(
									'ctime'=>time(),
									'truename'=>$post['truename'],
									'xingbie'=>$post['xingbie'],
									'shengri'=>$post['shengri'],
									'shengao'=>$post['shengao'],
									'tizhong'=>$post['tizhong'],
									'hunyin'=>$post['hunyin'],
									'hpro'=>$post['hpro'],
									'hcity'=>$post['hcity'],
									'pro'=>$post['pro'],
									'city'=>$post['city'],
									'area'=>$post['area'],
									'xueli'=>$post['xueli'],
									'phone'=>$post['phone'],
								);
				$user_info_status = D('member_info')->where('uid = '.$_SESSION['wxuser']['uid'])->save($user_info);
				if($jobs_resume_status&&$user_info_status){
					return true;
				}else{
					return false;
				}
			}
			if($step==2){
				$jobs_resume = array(
									'yhyf'=>implode(',',$post['hangye']),
								);
				$jobs_resume_status = D('resume')->where('uid = '.$_SESSION['wxuser']['uid'])->save($jobs_resume);
				if($jobs_resume_status){
					return true;
				}else{
					return false;
				}
			}
			if($step==3){
				foreach($post['worklist'] as $k => $v){
					$jobs_exper = array(
										'ctime'=>time(),
										'jobbegin'=>$v['jobbegin'],
										'jobend'=>$v['jobend'],
										'jobzhiwei'=>$v['jobzhiwei'],
										'jobname'=>$v['jobname'],
									);
					D('exper')->where('in_id = '.$k)->save($jobs_exper);
				}
				return true;
			}
			if($step==4){
				$jobs_resume = array(
									'ctime'=>time(),
									'edubeigntime'=>$post['edubegintime'],
									'eduendtime'=>$post['eduendtime'],
									'eduschool'=>$post['eduschool'],
									'eduxueli'=>$post['eduxueli']
								);
				$jobs_resume_status = D('resume')->where('uid = '.$_SESSION['wxuser']['uid'])->save($jobs_resume);
				if($jobs_resume_status){
					return true;
				}else{
					return false;
				}
			}
			if($step==6){
				$jobs_resume['myinfo'] = $post['myinfo'];
				$jobs_resume_status = D('resume')->where('uid = '.$_SESSION['wxuser']['uid'])->save($jobs_resume);
				if($jobs_resume_status){
					return true;
				}else{
					return false;
				}
			}
		}
		
		public function addworklist($post){
			$jobs_exper = array(
								'uid'=>$_SESSION['wxuser']['uid'],
								'ctime'=>time(),
								'jobbegin'=>$post['jobbegin'],
								'jobend'=>$post['jobend'],
								'jobzhiwei'=>$post['jobzhiwei'],
								'jobname'=>$post['jobname'],
							);
			$jobs_exper_status = D('exper')->add($jobs_exper);
			if($jobs_exper_status){
				return true;
			}else{
				return false;
			}
		}
		
		public function postresume($jobid){
			$resume = D('jobs_resume')->where('uid = '.$_SESSION['wxuser']['uid'])->find();
			$title =  D('jobs')->where(array('jobs_id'=>$jobid))->getField('title');
			$com_uid = D('jobs')->where(array('jobs_id'=>$jobid))->getField('uid');
      		 $touser = D('user')->where(array('uid'=>$com_uid))->getField('service_wx');
      		$uui = array('uid'=>$_SESSION['wxuser']['uid']);
	      $uinfo = D('user_info')->where($uui)->find();
	      $xue = D('user_xueli')->getField('x_id,x_name');
	      $xueli = $xue[$uinfo['xueli']];
	      $area = D('area')->getField('area_id,title');
	      $city = $area[$uinfo['pro']].'-'.$area[$uinfo['city']].'-'.$area[$uinfo['area']];
	      $uresume = D('jobs_resume')->where($uui)->find();
	      $uexp = D('jobs_exper')->where($uui)->order('ctime desc')->find();
	      if($uresume['jingyan'] == 0 or empty($uresume['jingyan'])){
	        $jingyan = "无经验";
	      }else{
	        $jingyan = $uresume['jingyan']."年";
	      }
	      if(empty($uexp['jobname'])){
	        $exps = "未提供";
	      }else{
	        $exps = $uexp['jobname'].$uexp['jobzhiwei'];
	      }
	      if(empty($uresume['eduschool'])){
	        $school = "未填写";
	      }else{
	        $school = $uresume['eduschool'];
	      }
			if(!empty($resume)){
				$postresume = array(
									'from' => 2,
									'jobs_id' => $jobid,
									'resume_id' => $resume['resume_id'],
									'send_time' => time(),
									'is_check'=>0,
									'is_del'=>0
								);
				$postresume_status = D('send_resume')->add($postresume);
				
				if($postresume_status){
					if(!empty($touser)){
			          $Wei = D('weixin');
			        	$Wei ->send_resume_weixin('http://115.29.250.19/service_weixin/index.php?s=/wxweb/resume_detail/rid/'.$resume['uid'],$touser,$title,
			          $uinfo['truename'],$xueli,$jingyan,$city,$exps,
			         $school,'正在找工作，可立即上岗',$uinfo['phone']);
			        }
					return 1;
				}else{
					return 0;
				}
			}else{
				return -1;
			}
		}
		
		public function getsendresume(){
			$myresume = D('jobs_resume')->where('uid = '.$_SESSION['wxuser']['uid'])->find();
			$jl = D('send_resume')->where('resume_id = '.$myresume['resume_id'])->order('send_time DESC')->findPage(30);
			foreach($jl['data'] as $k => &$v){
				$v['jobs'] = D('jobs')->where('jobs_id = '.$v['jobs_id'])->find();
				$v['jobs']['company'] = getcompanybycid($v['jobs']['company_id']);
				$v['jobs']['jobtype'] = getjobtype($v['jobs']['jobtypec']);
			}
			return $jl;
		}
		
		public function resetpwd($pwd,$name){
			$user = D('user')->where('login = "'.$name.'"')->find();
			$data['password'] = md5(md5($pwd).$user['login_salt']);
			if(D('user')->where('login = "'.$name.'"')->save($data)){
				return true;
			}else{
				return false;
			}
		}
		
		public function getmycustom(){
			$uid = $_SESSION['wxuser']['uid'];
			$result = D('custom')->where('uid = '.$uid)->find();
			return $result;
		}
		
		public function initmycustom(){
			$data['uid'] = $_SESSION['wxuser']['uid'];
			$data['updatetime'] = time();
			D('custom')->add($data);
		}
		
		public function updatecustom($post){
			$uid = $_SESSION['wxuser']['uid'];
			$data['hangye'] = implode(',',$post['hangye']);
			$selectnum = count($post['workplace']['pro']);
			for($i=0;$i<$selectnum;$i++){
				$workplace[$i] = array(
											'pro' => $post['workplace']['pro'][$i],
											'city' => $post['workplace']['city'][$i],
											'area' => $post['workplace']['area'][$i]
										);
			}
			$data['workplace'] = serialize($workplace);
			$data['worktime'] = implode(',',$post['worktime']);
			$data['jobstype'] = implode(',',$post['position']);
			$data['updatetime'] = time();
			D('custom')->where('uid = '.$uid)->save($data);		
		}
		
		public function getwheremylist($post){
			if(!empty($post['hangye'])&&$post['hangye'][0]!=17){
				$where[]='`jobtypec` IN ('.implode(',',$post['hangye']).')';
			}
			if(!empty($post['worktime'])){
				$where[]='`jobstime` IN ('.implode(',',$post['worktime']).')';
			}
			if(!empty($post['hangye'])){
				$coms = D('com_info')->where('company_id IN('.implode(',',$post['hangye']).')')->findAll();
				foreach($coms as $k => $v){
					$hangye_comids[] = $v['company_id'];
				}
			}
			if(!empty($post['workplace'])){
				$coms = D('store')->where('area IN ('.implode(',',$post['workplace']['area']).')')->findAll();
				foreach($coms as $k => $v){
					$wp_comids[] = $v['company_id'];
				}
			}
			$comids = array_merge($hangye_comids,$wp_comids);
			if(!empty($comids)){
				$where[]='`company_id` IN ('.implode(',',$comids).')';
			}
			$mylist = D('jobs')->where('('.implode(' OR ',$where).') AND status = 1')->findAll();
			$myresume = D('jobs_resume')->where('uid = '.$_SESSION['wxuser']['uid'])->find();
			$sended = D('send_resume')->where('resume_id = '.$myresume['resume_id'])->findAll();
			foreach($sended as $k => $v){
				$sendids[] = $v['jobs_id'];
			}
			foreach($mylist as $k => &$v){
				$v['company'] = getcompanybycid($v['company_id']);
				$v['jobtype'] = getjobtype($v['jobtypec']);
				$v['jobtime'] = getworktime($v['jobstime']);
				switch($v['xingbie']){
					case 1:$v['sex'] = '男';break;
					case 2:$v['sex'] = '女';break;
					case 3:$v['sex'] = '无要求';break;
				}
				$v['xueli_name'] = getxueli($v['xueli']);
				$v['workstore'] = getworkstore($v['store']);
				if(in_array($v['jobs_id'],$sendids)){
					$v['sended'] = 1;
				}
				if($v['salary']!='面议'){
					$v['salary'].='元';
				}
			}
			//dump($mylist);
			return $mylist;
		}
	
	}
?>